<?php

/**
 * 安心站长axzz.cn，一个追求技术创造者。让网站、小程序、APP牛起来……
 *
 * cighsen02 365182575
 * 
 * Create at: 2025.08.23
 * Update at: 2025.09.03
 */

namespace Twoir\Service\ShowKey;

use Exception;

use function array_key_exists;
use function base_convert;
use function ehtmlspecialchars;
use function extension_loaded;
use function str_replace;
use function substr;

/**
 * 验证码基类
 */
abstract class BaseImage implements OutputInterface
{
    protected int $width;

    protected int $height;

    protected int $fontsize;

    protected int $keytog;
    // array 背景色
    protected array $bgcolor;
    // array 文字色
    protected array $fontcolor;
    // array 干扰色
    protected array $distcolor;

    protected string $imageType;
    /**
     * @param string $imageType
     * @param int $width
     * @param int $height
    */
    public function __construct(string $imageType = 'jpeg', int $width = 47, int $height = 20, $fontsize = 6)
    {
        global $public_r;

        if(!extension_loaded('gd')) throw new Exception('请先安装gd库！', 9200);

        if (empty($imageType) || !array_key_exists($imageType, static::MIME)) throw new Exception('不支持类型', 9200);

        $this->width = $width ?? static::WIDTH;

        $this->height = $height ?? static::HEIGHT;

        $this->fontsize = $fontsize ?? static::FONT_SIZE;

        $this->keytog = (int)$public_r['keytog'];
        // 背景色
        $this->bgcolor = isset($public_r['keybgcolor']) && $public_r['keybgcolor'] ? $this->returnRGB($public_r['keybgcolor']) : static::DEFAULT_RGB['keybgcolor'];
        // 文字色
        $this->fontcolor = isset($public_r['keyfontcolor']) && $public_r['keyfontcolor'] ? $this->returnRGB($public_r['keyfontcolor']) : static::DEFAULT_RGB['keyfontcolor'];
        // 干扰色
        $this->distcolor = isset($public_r['keydistcolor']) && $public_r['keydistcolor'] ? $this->returnRGB($public_r['keydistcolor']) : static::DEFAULT_RGB['keydistcolor'];
        // 统一
        if('jpg' === $imageType) $imageType = 'jpeg';

        $this->imageType = $imageType;
    }

    /**
     * 验证码渲染
     *
     * @param array $add
     * @return string
     *
     * @throws Exception
    */
    abstract public function render(array $add): string;

    /**
     * 生成图像
     *
     * @param array $add
     * @param bool $imageBase64
     * @return string
     * 
     * @throws Exception
    */
    abstract public function base64DataURI(array $add, bool $imageBase64 = true): string;

    /**
     * 字体盒子计算
     * 
     * @param array
     * @return array
     */
    protected function imagettfbboxCalc($bbox)
    {
        return [
            'x' => ($this->width - $bbox[2] + $bbox[0]) / 2,
            'y' => ($this->height - $bbox[1] + $bbox[5]) / 2 - $bbox[5]
        ];
    }

    /**
     * 输出图片类型
     * 
     * @return string
     */
    public function getContentType(): string
    {
        return static::MIME[$this->imageType];
    }

    /**
     * 把16进制转10进制
     *
     * @param string $rgb
     * @return array
    */
    protected function returnRGB(string $rgb): array
    {
        $rgb = str_replace('#', '', ehtmlspecialchars($rgb));

        return [
            base_convert(substr($rgb, 0, 2), 16, 10),
            base_convert(substr($rgb, 2, 2), 16, 10),
            base_convert(substr($rgb, 4, 2), 16, 10)
        ];
    }
}
